#!/usr/bin/python

# InnerSpace
# Copyright (C) 1999-2006 Phil Christensen
#
# See LICENSE for details

import unittest, string, sys, os, os.path

from inner.space import registry, parser, errors, minimal

class ParserTestCase(unittest.TestCase):
	def setUp(self):
		self.registry = registry.Registry()
		minimal.init(self.registry, os.path.join(os.getcwd(), 'verbs'))
		self.wizard = self.registry.get("wizard")
		self.mask = self.registry.get("mask")
		self.room = self.registry.get("The Classroom")
	
	def tearDown(self):
		self.registry = None
		self.wizard = None
		self.wizard = None
		self.mask = None
		self.room = None
	
	def test_parse_verb(self):
		p = parser.Parser(parser.Lexer("look"), self.wizard, self.registry)
		assert not p.has_dobj(), "unexpected object found for dobj"
		assert not p.has_dobj_str(), "unexpected string found for dobj"
		assert not p.prepositions, "unexpected prepositional objects found"
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj)
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj, "on")
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj_str)
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj_str, "on")
	
	def test_parse_verb_dobj(self):
		p = parser.Parser(parser.Lexer("look wizard"), self.wizard, self.registry)
		assert p.has_dobj(), "dobj 'wizard' not found"
		assert p.has_dobj_str(), "dobj string 'wizard' not found"
		assert not p.prepositions, "unexpected prepositional objects/strings found"
		self.assertEqual(p.get_dobj(), self.wizard)
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj, "on")
		self.assertEqual(p.get_dobj_str(), self.wizard.get_name())
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj_str, "on")
		
	def test_parse_verb_pobj(self):
		p = parser.Parser(parser.Lexer("look through peephole"), self.wizard, self.registry)
		assert not p.has_dobj(), "unexpected object found for dobj"
		assert not p.has_dobj_str(), "unexpected string found for dobj"
		assert p.has_pobj_str('through'), "no prepositional object string found for 'through'"
		assert not p.has_pobj('through'), "no prepositional object found for 'through'"
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj)
		self.assertRaises(errors.NoSuchObjectError, p.get_pobj, "through")
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj_str)
		self.assertEqual(p.get_pobj_str("through"), "peephole")
		
	def test_parse_verb_pobj_pobj(self):
		p = parser.Parser(parser.Lexer("look through peephole with mask"), self.wizard, self.registry)
		assert not p.has_dobj(), "unexpected object found for dobj"
		assert not p.has_dobj_str(), "unexpected string found for dobj"
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj)
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj, "from")
		self.assertEqual(p.get_pobj("with"), self.mask)
		self.assertEqual(p.get_pobj_str("with"), "mask")
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj_str)
		self.assertEqual(p.get_pobj_str("through"), "peephole")
	
	def test_parse_verb_dobj_pobj(self):
		p = parser.Parser(parser.Lexer("take glasses from wizard with tongs"), self.wizard, self.registry)
		self.assertRaises(errors.NoSuchObjectError, p.get_dobj)
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj, "under")
		self.assertEqual(p.get_dobj_str(), "glasses")
		self.assertEqual(p.get_pobj_str("with"), "tongs")
		self.assertEqual(p.get_pobj("from"), self.wizard)
		self.assertEqual(p.get_pobj_str("from"), "wizard")
	
	def test_complex(self):
		p = parser.Parser(parser.Lexer("take the mask from 'bag under stairs' with tongs in wizard's bag"), self.wizard, self.registry)
		self.assertRaises(errors.NoSuchObjectError, p.get_pobj, "from")
		self.assertRaises(errors.NoSuchObjectError, p.get_pobj, "with")
		self.assertEqual(p.get_pobj_str('from'), 'bag under stairs')
		self.assertEqual(p.get_dobj(), self.mask)
		self.assertEqual(p.get_pobj_str("with"), "tongs")
	
	def test_quoted_strings(self):
		p = parser.Parser(parser.Lexer("@passwd wizard to 'something here'"), self.wizard, self.registry)
		self.assertEqual(p.get_pobj_str('to'), 'something here')
	
	def test_bug_9(self):
		p = parser.Parser(parser.Lexer("@describe here as 'Large amounts of chalkdust lay all over the objects in this room, and a large chalkboard at one end has become coated with a thick layer of Queen Anne\\'s lace. Strange semi-phosphorescant orbs are piled all around this ancient hall.'"), self.wizard, self.registry)
		self.assertRaises(errors.NoSuchPrepositionError, p.get_pobj_str, 'around')
		assert "\\" not in p.get_pobj_str('as')
		
if __name__ == "__main__":
	unittest.main()
